package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.DeliveryPlanDao;
import cn.gson.crm.model.dao.DeliveryScheduleDetailsDao;
import cn.gson.crm.model.dao.OutboundDao;
import cn.gson.crm.model.dao.WaouDetailsDao;
import cn.gson.crm.model.mapper.DeliveryPlanMapper;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.pojos.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class DeliveryPlanService {
    @Autowired
    DeliveryPlanMapper deliveryPlanMapper;
    @Autowired
    DeliveryPlanDao deliveryPlanDao;
    @Autowired
    DeliveryScheduleDetailsDao deliveryScheduleDetailsDao;
    @Autowired
    OutboundDao outboundDao;
    @Autowired
    WaouDetailsDao waouDetailsDao;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //查询当前登录人所有交付计划
    public Map<String,Object> selectdel(HttpSession session,Integer pid,Integer page, Integer size){
        pid=fouyuan(session);
        List<DeliveryPlan> deliveryPlans2=deliveryPlanMapper.selectdel(pid);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<DeliveryPlan> deliveryPlans=deliveryPlanMapper.selectdel(pid);
        //单号
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        User login = (User)session.getAttribute("login");
        int ygId= (int) login.getEmployees().get(0).getYgId();
        //查询当前人下所有客户
        List<Customer>  customerList=deliveryPlanMapper.selectcustomer(ygId);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",deliveryPlans);
        map.put("customerList",customerList);
        map.put("number","JF"+newDate+result);
        map.put("chukudan","CK"+newDate+result);
        map.put("tong",deliveryPlans2);
        return map;
    };
    //多条件查询
    public  Map<String,Object> selectwhere(HttpSession session, Integer pid, String where,Integer page, Integer size){
        pid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<DeliveryPlan>  deliveryPlans=deliveryPlanMapper.selectwhere(pid,where);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",deliveryPlans);
        return map;
    };
    //时间查询
    public Map<String,Object> selecttime( HttpSession session,Integer pid,String statr,String endtime,Integer page, Integer size){
        pid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<DeliveryPlan>  deliveryPlans=deliveryPlanMapper.selecttime(pid,statr,endtime);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",deliveryPlans);
        return map;
    };
    //查询当前人下所有客户
    public List<Customer> selectcustomer(Integer yg){
        return deliveryPlanMapper.selectcustomer(yg);
    };
    //根据客户id查询没有被退换并且状态是发货的的订单号
    public List<Order> selectorder(Integer cid){
        return deliveryPlanMapper.selectorder(cid);
    };
    //根据id查询订单详情
    public List<OrderDetails> selectoid(Integer oid){
        List<DeliveryScheduleDetails> deliveryPlans=deliveryPlanMapper.xiangqin(oid);
       List<OrderDetails> orderList= deliveryPlanMapper.selectoid(oid);
       if(deliveryPlans!=null||deliveryPlans.size()>0){
           for(int i=0;i<orderList.size();i++){
               for(int y=0;y<deliveryPlans.size();y++){
                   if(orderList.get(i).getOrderdName().equals(deliveryPlans.get(y).getDsdRoductName()) &&
                           orderList.get(i).getOrderdCompany().equals(deliveryPlans.get(y).getDsdCompany())&&
                           orderList.get(i).getOrderdSize().equals(deliveryPlans.get(y).getDsdSize())){
                       orderList.get(i).setOrderdNumber((int) (orderList.get(i).getOrderdNumber()-deliveryPlans.get(y).getDsdNumber()));

                   }
               }
           }

       }
       for(int c=0;c<orderList.size();c++){
           if(orderList.get(c).getOrderdNumber()<=0){
               orderList.remove(c);
           }
       }
       return orderList;
    };
    //新增
    public void sacedel(DeliveryPlan deliveryPlan){
        if(deliveryPlan.getPlanId()!=0){
            deliveryPlanDao.save(deliveryPlan);
        }else {
            deliveryPlanDao.save(deliveryPlan);
            deliveryPlan.getDeliveryScheduleDetails().forEach(p->{
                p.setDeliveryPlanByPlanId(deliveryPlan);
            });
            deliveryScheduleDetailsDao.saveAll(deliveryPlan.getDeliveryScheduleDetails());
            int zon=0;
            List<Order> orders=deliveryPlanMapper.ordermoney((int) deliveryPlan.getOrderByOrderId().getOrderId());
            List<DeliveryPlan> deliveryPlans=deliveryPlanMapper.skod((int)deliveryPlan.getOrderByOrderId().getOrderId());

                for(int i=0;i<deliveryPlans.size();i++){
                    zon+=deliveryPlans.get(i).getRecordMoney();
                }
                //判断如果金额等于订单总金额那就修改
                zon= (int) (zon+deliveryPlan.getRecordMoney());
                System.out.println(zon+"---------------------------");
                System.out.println(deliveryPlan.getRecordMoney());
                if(zon==orders.get(0).getOrderMoney()){
                    deliveryPlanMapper.updateorder((int)deliveryPlan.getOrderByOrderId().getOrderId());
                }

        }
    }
    //查询详情
    public List<DeliveryScheduleDetails> dedeta(Integer did){
        return  deliveryPlanMapper.dedeta(did);
    }
}
